package org.uiot.protocol.codec

//import grails.databinding.BindUsing

trait CodecTraits {
    static enum ENDIAN {
        LITTLE(0), BIG(1)
        final int value

        private ENDIAN(int value) { this.value = value }

        static ENDIAN byValue(int val) {
            values().find { it.value == val } ?:
                    { throw new IllegalArgumentException("BasicRule\$ENDIAN ${val} not found") }()
        }
    }

    static enum LengthUnit {
        Bit(0), Byte(1)
        final int value

        private LengthUnit(int value) { this.value = value }

        static LengthUnit byValue(int val) {
            values().find { it.value == val } ?:
                    { throw new IllegalArgumentException("BasicRule\$LengthUnit ${val} not found") }()
        }
    }

    static enum Type {
        Numeric(0), StringCompressedBCD(1), StringUncompressedBCD(2), StringASCII(3), StringUTF8(4), StringGBK(5)
        final int value

        private Type(int value) { this.value = value }

        static Type byValue(int val) {
            values().find { it.value == val } ?:
                    { throw new IllegalArgumentException("BasicRule\$Type ${val} not found") }()
        }
    }

    /**
     * 代码
     */
    String code

    /**
     * 名称
     */
    String name

    /**
     * 字节序
     */
//    @BindUsing({ obj, source ->
//        ENDIAN.byValue(Integer.parseInt(source["endian"] as String))
//    })
    ENDIAN endian

    /**
     * 长度
     */
    Integer length
    /**
     * 长度单位
     */
//    @BindUsing({ obj, source ->
//        LengthUnit.byValue(Integer.parseInt(source["lengthUnit"] as String))
//    })
    LengthUnit lengthUnit

    /**
     * 数据类型
     */
//    @BindUsing({ obj, source ->
//        Type.byValue(Integer.parseInt(source["type"] as String))
//    })
    Type type
}